package com.lightsaber.trade.cloth.dao.planning;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.cloth.entity.planning.Level2Planning;
import com.lightsaber.trade.cloth.entity.planning.ProgrammePlanningItem;

@Component
public class ProgrammePlanningItemDao extends BaseDao<ProgrammePlanningItem, String> {
    final static Logger log = LoggerFactory.getLogger(ProgrammePlanningItemDao.class);

    public ProgrammePlanningItem findUniquePpi(final String level2PlanningId, final String code,
            final Integer planYear, final Integer studyYear, final String operatingCampus,
            final String operatingUnit) {
        eq("level2Planning.id", level2PlanningId);
        eq("code", code);
        eq("operatingCampus", operatingCampus);
        eq("operatingUnit", operatingUnit);
        eq("planYear", planYear);
        eq("studyYear", studyYear);
        return unique();
    }

    public long sumPlanplaceByPlanningAndCode(final Level2Planning level2Planning, final String code) {

        String hql = "select sum(ppi.plannedPlacesNew+ppi.plannedPlacesProg) from ProgrammePlanningItem as ppi"
                + " where ppi.level2Planning=:level2Planning and code=:code"
                + " and planYear=:planYear";

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("level2Planning", level2Planning);
        map.put("code", code);
        map.put("planYear", level2Planning.getPlanningExercise());

        Query query = this.createQuery(hql, map);
        Object result = query.uniqueResult();
        return result == null ? 0 : (Long) result;
    }

    public List<Long> sumForPlannedPlaceByLevel2IdAndCampusIdAndPlanYear(
            final String level2PlanningId, final String campusId, final Integer planYear) {

        String hql = "select sum(ppi.plannedPlacesNew) as newPlannedPlace, sum(ppi.plannedPlacesProg) as progPlannedPlace from ProgrammePlanningItem ppi "
                + " where ppi.level2Planning.id=:level2PlanningId "
                + " and ppi.offeringCampus.id=:campusId and ppi.planYear=:planYear ";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("level2PlanningId", level2PlanningId);
        map.put("campusId", campusId);
        map.put("planYear", planYear);

        Query query = this.createQuery(hql, map);
        query.setResultTransformer(Transformers.TO_LIST);
        return (List) query.list().get(0);
    }

    public boolean existsCode(final String programmeCode) {
        eq("code", programmeCode);

        long cnt = count();

        log.info("Has {} Programme Planning Item which code is {}.", cnt, programmeCode);

        if (cnt > 0) {
            return true;
        }

        return false;
    }
}
